//
//  HMStatusToolBar.swift
//  Faner_Weibo
//
//  Created by 范英强 on 16/2/1.
//  Copyright © 2016年 范英强. All rights reserved.
//

import UIKit

class HMStatusToolBar: UIView {
    
    var retweetButton: UIButton?
    var commentButton: UIButton?
    var unlikeButton: UIButton?
    
    var statusViewModel: HMStatusViewModel?{
        didSet{
            
            retweetButton?.setTitle(statusViewModel?.retweetCountStr, forState: UIControlState.Normal)
            commentButton?.setTitle(statusViewModel?.commentCountStr, forState: UIControlState.Normal)
            unlikeButton?.setTitle(statusViewModel?.unlikeCountStr, forState: UIControlState.Normal)
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        retweetButton = addChildButton("timeline_icon_retweet", title: "转发")
        commentButton = addChildButton("timeline_icon_comment", title: "评论")
        unlikeButton = addChildButton("timeline_icon_unlike", title: "赞")
        let sp1 = AddSeparator()
        let sp2 = AddSeparator()
        
         retweetButton?.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self)
            make.top.equalTo(self)
            make.bottom.equalTo(self)
            make.width.equalTo(commentButton!)
        }
        
        commentButton?.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(retweetButton!.snp_trailing)
            make.top.equalTo(self)
            make.bottom.equalTo(self)
            make.width.equalTo(unlikeButton!)
        }
        
        unlikeButton?.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(self)
            make.bottom.equalTo(self)
            make.trailing.equalTo(self)
            make.leading.equalTo(commentButton!.snp_trailing)
        }
        
        sp1.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(retweetButton!.snp_trailing)
            make.centerY.equalTo(self)
        }
        
        sp2.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(commentButton!.snp_trailing)
            make.centerY.equalTo(self)
        }
        
    }
    
    private func addChildButton(imgName:String,title: String) -> UIButton{
        let button = UIButton()
        button.setTitle(title, forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        button.setTitleColor(UIColor.grayColor(), forState: UIControlState.Normal)
        button.setImage(UIImage(named: imgName), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "timeline_card_bottom_background"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "timeline_card_bottom_background_highlighted"), forState: UIControlState.Highlighted)
        addSubview(button)
        return button
    }
    
    private func AddSeparator() -> UIView{
        let imageView = UIImageView(image: UIImage(named: "timeline_card_bottom_line"))
        addSubview(imageView)
        return imageView
    }
}
